﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Aspose.Cells;
using Aspose.Words;
using Aspose.Words.Saving;
using PdfSaveOptions = Aspose.Words.Saving.PdfSaveOptions;
using SaveFormat = Aspose.Words.SaveFormat;
using SaveOptions = Aspose.Cells.SaveOptions;

namespace AsposeWordSample.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult call()
        {
            Workbook workbook = new Workbook("d:\\t.xls.xlsx");


            //Save the document in PDF format
            //var stream = new FileStream("f:\\test\\outBook1.pdf",FileMode.Create);
            
             workbook.Save(@"c:\\a.pdf", FileFormatType.Pdf);
           // workbook.Save("dd", fileFormatType.pd);
            return null;
        }

        public ActionResult ExcelToPdf()
        {
            var workbook = new Workbook(Server.MapPath("~/template/1.xlsx"));
            var checkoutDetail = workbook.Worksheets["sheet1"];
            checkoutDetail.Cells[0, 1].PutValue("DBDdkkdkdkf");//批次号


            PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails("", "kk", PdfEncryptionAlgorithm.RC4_128);
            encryptionDetails.Permissions = PdfPermissions.Printing;
            Aspose.Cells.PdfSaveOptions saveOptions = new Aspose.Cells.PdfSaveOptions ();

            saveOptions.SecurityOptions = new Aspose.Cells.Rendering.PdfSecurity.PdfSecurityOptions();
            //Set the user password
            saveOptions.SecurityOptions.UserPassword = "";

            //Set the owner password
            saveOptions.SecurityOptions.OwnerPassword = "owner";

            var path = Server.MapPath("~");

            workbook.Save(path + "\\" + "b.pdf", saveOptions);
            return null;

        }
        public ActionResult WordToPdf()
        {
            Document doc = new Document(Server.MapPath("~/template/第三方银行账户授权委托书.doc"));
            DocumentBuilder builder = new DocumentBuilder(doc);

            Bookmark mark = doc.Range.Bookmarks["CompanyName"];

            mark.Text = "小李子小李子小李子小李子小" +
                        "李子小李子小李子小李子小李子" +
                        "小李子小李子小李子小李子小李子" +
                        "小李子小李子小李子小李子小李子小李子小李子小李子小李子小李子" +
                        "小李子小李子小李子小李子小李子小李子小李子小李子小李子小李子小李子小李子小李子";
            var path = Server.MapPath("~");
            PdfEncryptionDetails encryptionDetails = new PdfEncryptionDetails("", "kk", PdfEncryptionAlgorithm.RC4_128);
            encryptionDetails.Permissions = PdfPermissions.Printing;
            PdfSaveOptions saveOptions = new PdfSaveOptions();

            saveOptions.EncryptionDetails = encryptionDetails;

            doc.Save(path + "\\" + "a.pdf", saveOptions);
            return null;
        }
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            Document doc = new Document(Server.MapPath("~/template/t1.doc"));
            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.Writeln("Hello,Word!");  //插入文本  

            Bookmark mark = doc.Range.Bookmarks["Text1"];

            var path = Server.MapPath("~");
            mark.Text = "小李子";
            var imagePath = path + @"\content\images\1.jpg";

        
            mark = doc.Range.Bookmarks["idimg"]; //定位图片
            //var path = HttpContext.Current.Server.MapPath("~");

            builder.MoveTo(mark.BookmarkStart);
            mark.Text = string.Empty;
            builder.InsertImage(imagePath);

           
            //doc.Save(path+"\\" + "result.doc");
            doc.Save(path + "\\" + "a.jpg", SaveFormat.Jpeg);
            
            return null;
        }

        public ActionResult About()
        {
            return View();
        }
    }
}
